<?php

namespace App\Models\Goods;

use DB;

use Carbon\Carbon;

use Illuminate\Database\Eloquent\Model;

use App\Models\Goods\GoodsBrand;

use App\Models\Goods\GoodsUnit;


class GoodsImport extends Model
{

    protected $table = 'goods_import';

    protected $primaryKey = 'importID';

    public $timestamps = false;

    public $incrementing = false;


    //商品录入保存
    public static function store( $product_data )
    {

        if ( !isset( $product_data ) || empty( $product_data ) ) {
            return ['code' => 10000, 'message' => '商品信息不存在'];
        }

        /**
         * =====================================================
         * 检查商品基础信息
         * =====================================================
         */
        $product_data['product_name'] = htmlspecialchars( trim( $product_data['product_name'] ) );
        $product_data['brief'] = htmlspecialchars( trim( $product_data['brief'] ) );

        $product['brand_id'] = 0;
        $product['brand_name'] = '';
        $product['unit_id'] = 0;
        $product['unit_name'] = '';

        //商品品牌(如果存在则查询)
        if ( isset( $product_data['brand_name'] ) && !empty( $product_data['brand_name'] ) ) {
            $goods_brand = GoodsBrand::select('brandID','brandName')->where( ['brandName'=> $product_data['brand_name'] ] )->get()->toArray();
            if ( !$goods_brand ) {
                return ['code' => 10001, 'message' => '商品品牌信息没有找到'];
            }
            $product['brand_id'] = $goods_brand[0]['brandID'];
            $product['brand_name'] = $goods_brand[0]['brandName'];
        }
        //商品单位(如果存在则查询)
        if ( isset( $product_data['unit_name'] ) && !empty( $product_data['unit_name'] ) ) {
            $goods_unit = GoodsUnit::select('unitID','unitName')->where( ['unitName'=> $product_data['unit_name'] ] )->get()->toArray();
            if ( !$goods_unit ) {
                return ['code' => 10002, 'message' => '商品单位信息没有找到'];
            }
            $product['unit_id'] = $goods_unit[0]['unitID'];
            $product['unit_name'] = $goods_unit[0]['unitName'];
        }

        //商品价格数组
        $product_sale_data = [];

        foreach ( $product_data['sale'] as &$sale ) {

            $product_sale_data[] = [
                'product_code' => $sale['product_code'],
                'salePrice' => $sale['sale_price'],
                'marketPrice' => $sale['market_price'],
                'costPrice' => $sale['cost_price'],
                'ceilingPrice' => $sale['higher_price'],
                'lowerPrice' => $sale['lower_price'],
                'goodsWeight' => 0,
                'salePoints' => $sale['sale_points'],
                'product_spec' => 1,
                'virtual_id' => 0,
                'virtual_name' => '',
                'goodsRate' => $sale['product_rate'],
                'recordPrice' => $sale['record_price'],
                'record_code' => $sale['record_code'],
                'customs_product_code' => $sale['customs_product_code'],
                'spec' => '',
                'specID' => '',
                'id' => 0
            ];
        }

        $product_json = [
            'spec_type' => 0,
            'goods_spec_relation' => [],
            'goods_sale_data' => $product_sale_data,
            'goods_master_data' => [
                'goodsName' => $product_data['product_name'],
                'goodsType' => $product_data['product_type'],
                'spec_type' => 0,
                'goods_limit' => 0,
                'bigCategoryID' => 0,
                'midCategoryID' => 0,
                'smallCategoryID' => 0,
                'brandID' => $product['brand_id'],
                'brandName' => $product['brand_name'],
                'unitID' => $product['unit_id'],
                'unitName' => $product['unit_name'],
                'brief' => $product_data['brief'],
                'PC' => 1,
                'WAP' => 1,
                'APP' => 1,
                'spec_pic_type' => 0
            ]
        ];

        try {

            DB::beginTransaction();

            //保存主商品信息
            $goods_import_obj = new GoodsImport;
            $goods_import_obj->uuid = makeUuid();
            $goods_import_obj->timeStamp = Carbon::now();
            $goods_import_obj->creator = 'api';
            $goods_import_obj->createTime = Carbon::now();
            $goods_import_obj->goodsName = $product_data['product_name'];
            $goods_import_obj->marketPrice = $product_data['sale'][0]['market_price'];
            $goods_import_obj->salePrice = $product_data['sale'][0]['sale_price'];
            $goods_import_obj->product_code_str = $product_data['sale'][0]['product_code'];
            $goods_import_obj->brandID = $product['brand_id'];
            $goods_import_obj->brandName = $product['brand_name'];
            $goods_import_obj->bigCategoryID = 0;
            $goods_import_obj->bigCategoryName = '';
            $goods_import_obj->midCategoryID = 0;
            $goods_import_obj->midCategoryName = '';
            $goods_import_obj->smallCategoryID = 0;
            $goods_import_obj->smallCategoryName = '';
            $goods_import_obj->goods_json = json_encode( $product_json );
            $goods_import_obj->save();

            DB::commit();

        } catch (\Exception $e) {

            DB::rollBack();

            return ['code' => 10003, 'message' => '批量添加商品失败' ];
        }

        return ['code' => 200, 'message' => 'ok' ];


    }



}
